package com.imodia.checkoo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.SqlParam;
import com.hsq.commons.util.tree.TreeNodeBean;
import com.imodia.checkoo.bean.CmsCatalogueBean;
import com.imodia.checkoo.bean.CmsDataDictionaryBean;
import com.imodia.checkoo.bean.CmsPageBean;
import com.imodia.checkoo.bean.CmsPageLayoutBean;
import com.imodia.checkoo.service.CmsCatalogueService;
import com.imodia.checkoo.service.CmsDataDictionaryService;
import com.imodia.checkoo.service.CmsPageLayoutService;
import com.imodia.checkoo.service.CmsPageService;
import com.imodia.checkoo.service.CmsSiteService;
import com.imodia.checkoo.thread.htmlInfo.PageTaskExecutorHtml;


@Controller
@RequestMapping("/hfmanager/page/list.do")
public class CmsPageController {
	
	private final Log log = LogFactory.getLog(CmsPageController.class);
	
	private final String baseUrl = "page";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit_redirect = "/hfmanager/" +baseUrl + "/list.do?action=edit";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";

	@Autowired
	private CmsPageService cmsPageService;
	@Autowired
	private CmsPageLayoutService cmsPageLayoutService;
	@Autowired
	private CmsCatalogueService cmsCatalogueService;
	@Autowired
	private CmsDataDictionaryService cmsDataDictionaryService;
	@Autowired
	private PageTaskExecutorHtml pageTaskExecutorHtml;
	@Autowired
	private CmsSiteService cmsSiteService;
	
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsPageService.delete(id);
		cmsCatalogueService.deleteByPageId(id);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsPageBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsPageService.update(obj);
		}else{
			x = cmsPageService.insert(obj);
			obj.setId(x);
		}
		pageTaskExecutorHtml.setRealPath(request.getRealPath("/"));
		pageTaskExecutorHtml.setSiteUrl("http://"+request.getServerName()+":"+request.getServerPort());
		pageTaskExecutorHtml.html4page(obj);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_edit_redirect+"&id="+obj.getId()+"&x="+x), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request,CmsPageBean obj) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();
		CmsPageBean o = cmsPageService.get(id);
		if(o!=null){
			CmsPageLayoutBean layout =  cmsPageLayoutService.get(o.getLayoutId());
			if(layout!=null){
				o.setLayoutImg(layout.getImgAddress());
			}
			List<TreeNodeBean> parent = cmsPageService.getTreePage2(o.getSiteId(),0,0);
			m.put("parent", parent);
		}
		if(o!=null){
			m.put("info", o);
		}else{
			m.put("info", obj);
		}
		
		
		m.put("siteInfo", cmsSiteService.list(new SqlParam()));
		

		List<CmsDataDictionaryBean> showTypeList = cmsDataDictionaryService.getClildInfo(1);
		m.put("showTypeList", showTypeList);
		
		List<CmsCatalogueBean> cmsCatalogueList = cmsCatalogueService.queryByPageId(id);
		m.put("catalogueList", cmsCatalogueList);
		
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsPageBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		int siteId = StringUtil.requestInt(request, "siteId");
		if(siteId==0)siteId=1;
		List<TreeNodeBean> list = cmsPageService.getTreePage2(siteId,0,0);
		model.put("list", list);
		
		model.put("siteInfo", cmsSiteService.list(new SqlParam()));
		
		return new ModelAndView(url_list, model);
	}
	
	

}
